// pages/ unlocking/ unlocking.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderid:"",
    count: 0, //计数器，初始值为0
    maxCount: 50, // 绘制一个圆环所需的步骤 
    countTimer: null, //定时器，初始值为null
    txt: "当前状态",
    txt2:"开锁失败",
    // 显示失败
    guzhang: -1,
    uqid: "",
    // ajax失败
    shibai: -1,
    numbering:"",
    circle1:"",

    orderstatus: ""
  },
  sleep(delay) {
    var start = (new Date()).getTime();
    while ((new Date()).getTime() - start < delay) {
      continue;
    }
  },


  userSubmit: function (e) {
    if (this.data.uqid) {
      console.log(e.detail.formId);
      ajax('/api/user/form_id', {
        data: {
          uqid: this.data.uqid,
          form_id: e.detail.formId
        },
        header: {
          'Accept': 'application/json', // 默认值
        },
        method: 'post'
      }, res => { })
    }
  },
  
  //定时画圆环
  countInterval: function() {

    for (let i = 0; i < 2 * this.data.maxCount + 1; i++) {
      if (this.data.count < 2 * this.data.maxCount - 10) {
        // 绘制彩色圆环进度条
        var count = this.data.count
        count += 2
        this.setData({
          count: count
        });
        console.log(this.data.count)
        this.circle1.drawCircle('circle_draw1', 115, 10, count / this.data.maxCount);
        this.sleep(25)

      } else if (this.data.count == 2 * this.data.maxCount - 10) {
        console.log(i + "次")
        // if (this.data.shibai == ""){
        //   do {
        //     console.log("this.data.shibai")
        //   }
        //   while (this.data.shibai != "") {
        //     break
        //   }
        // }
        
        if (this.data.shibai == 1) {
          this.setData({
            guzhang: 1
          });

          break

        } else if (this.data.shibai == 0) {
          this.setData({
            guzhang: 0
          });


         
          var count = this.data.count
          count += 1
          this.setData({
            count: count
          });
          console.log(this.data.count)
          this.circle1.drawCircle('circle_draw1', 115, 10, count / this.data.maxCount);
          this.sleep(50)
        }
      } else if (this.data.count > 2 * this.data.maxCount - 10 &&this.data.count < 2 * this.data.maxCount) {

        var count = this.data.count
        count += 1
        this.setData({
          count: count
        });
        console.log(this.data.count)
        this.circle1.drawCircle('circle_draw1', 115, 10, count / this.data.maxCount);
        this.sleep(50)
        if (this.data.count == 2 * this.data.maxCount){
          this.setData({
            txt: "开锁成功"
          });
        }
      }
    }

  },
  // drew:function(){
  //   while (this.data.count < 2 * this.data.maxCount){
  //     console.log(this.data.count)
  //     this.circle1.drawCircle('circle_draw1', 115, 8, this.data.count/this.data.maxCount);
  //   }
  // },
  onReady: function() {
    this.circle1 = this.selectComponent("#circle1");
    // 绘制背景圆环
    // this.circle1.drawCircleBg('circle_bg1', 115, 8)
    // 绘制彩色圆环 
    // this.circle1.drawCircle('circle_draw1', 115, 8, 1);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 获得circle组件
    var did = options.did

    this.circle1 = this.selectComponent("#circle1");
    console.log(this.circle1)
    this.sleep(100)


    var uqid = wx.getStorageSync("uqid")
    this.setData({
      did:did,
      uqid: uqid
    })

    console.log("kaishi  open")
    this.open()
    // this.countInterval()

  },

  // 开柜

  open: function() {
    ajax("/api/scan/unpacking", {
      data: {
        uqid: this.data.uqid,
        did: this.data.did,
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'POST'
    }, res => {
      console.log(res);
      if (res.error == 0) { //获取进展 pid
        this.setData({
          orderid: res.data.order_id,
          shibai:0
        });




      }
      if (res.error == 4017) {
        this.setData({
          shibai: 1,
          txt2:"开锁失败"
        });
      }
      if (res.error == 4015) {
        this.setData({
          shibai: 1,
          txt2: "消毒中"
        });
      }
      if (res.error == 4011) {
        this.setData({
          shibai: 1,
          txt2: "您有未完成的订单"
        });
        this.checkorder(this.data.uqid)

      }
      if (res.error == 4012) {
        this.setData({
          shibai: 1,
          txt2: "您有未支付的订单"
        });
        this.checkorder(this.data.uqid)
      }
      if (res.error == 4014) {
        this.setData({
          shibai: 1,
          txt2: "使用中"
        });
      }

      if (res.error == 4033) {
        this.setData({
          shibai: 1,
          txt2: "开锁失败"
        });
      }
      if (res.error == 4030) {
        this.setData({
          shibai: 1,
          txt2: "请先关门"
        });
      }
      console.log("jieshu open")
      this.countInterval()
    })

    // wx.request({
    //   url: 'https://pet.51appdevelop.com/api/scan/unpacking',
    //   data: {
    //     uqid: this.data.uqid,
    //     numbering: this.data.numbering,
    //   },
    //   header: {
    //     'Accept': 'application/json', // 默认值
    //   },
    //   method: 'POST',
    //   success: function (res) {
    //     console.log(res)
    //     if (res.data.error == 0) { //获取进展 pid
    //       this.setData({
    //         orderid: res.data.order_id,
    //         shibai: 0
    //       });
    //     }
    //     if (res.data.error == 4017) {
    //       this.setData({
    //         shibai: 1,
    //         txt2: "开锁失败"
    //       });
    //     }
    //     if (res.data.error == 4011) {
    //       this.setData({
    //         shibai: 1,
    //         txt2: "您有未完成的订单"
    //       });
    //     }
    //     if (res.data.error == 4012) {
    //       this.setData({
    //         shibai: 1,
    //         txt2: "开锁失败"
    //       });
    //     }
    //     if (res.data.error == 4014) {
    //       this.setData({
    //         shibai: 1,
    //         txt2: "使用中"
    //       });
    //     }
    //     console.log("jieshu open")
    //   }
    // })

    // this.countInterval()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  totopay: function() {

    ajax("/api/scan/placed", {
      data: {
        uqid: this.data.uqid,
        order_id: this.data.orderid
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'post'
    }, res => {
      console.log(res);
      if(res.error==0){
        wx.navigateTo({
          url: '/pages/storaging/storaging?orderid=' + this.data.orderid,
        })
      }
      if(res.error==4030){
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
      if (res.error == 4032) {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
     
    })

   
  },


  checkorder: function (uqid) {
    ajax('/api/index/undone_order', {
      data: {
        uqid: uqid
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {
      console.log(res)
      if (res.error == 0) {
        this.setData({
          orderstatus: res.data.code,
          orderid: res.data.order_id
        })
        // if (res.data.code == 1) {
        //   this.setData({
        //     messs: "您有正在进行的的订单",
        //     orderstatus: res.data.code
        //   })
        // }
        // if (res.data.code == 2) {
        //   this.setData({
        //     messs: "您有未支付的的订单",
        //     orderstatus: res.data.code
        //   })
        // }

      } else if (res.error == 4027) {
        this.setData({
          haveorder: 0
        })
      }
    })
  },


  tojishiye: function () {

    if (this.data.orderstatus == 1) {
      wx.navigateTo({
        url: '/pages/storaging/storaging?orderid=' + this.data.orderid,
      })
    }

    if (this.data.orderstatus == 2) {
      wx.navigateTo({
        url: '/pages/topay/topay?orderid=' + this.data.orderid,
      })
    }


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})